/*
 * @(#) SearchNews.java 1.0  2011-8-22 
 *
 * (c) Copyright 2011 smshen S3H3L3
 *
 * FILENAME    : SearchNews.java
 * PACKAGE     : com.sm.lucene
 * CREATE DATE : 2011-8-22
 * AUTHOR      : smshen
 */

package com.sm.lucene;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

import com.sm.model.News;
import com.sm.util.DataGrid;
import com.sm.util.JsonUtil;

/*******************************************************************************
 * #(c) smshen S3H3L3
 * 
 * 功能说明:根据关键词从索引文件中检索新闻
 * 
 * 2011-8-22 上午9:54:40 smshen 创建文件
 * 
 * 修改说明: 创建文件
 * 
 * 2011-8-22 上午9:54:40 smshen 修改文件
 * 
 ******************************************************************************/

public class SearchNews {
	
	/**
	 * CONTENT 新闻内容
	 */
	private static String CONTENT = "content";
	
	/**
	 * TEN 10
	 */
	private static final int TEN = 10;
	
	/**
	 * YBBS 180
	 */
	private static final int YBBS = 180;
	
	/**
	 * THIRTY 30
	 */
	private static final int THIRTY = 30;
	
	/**
	 * 
	 * 功能 :检索新闻
	
	 * 开发：smshen 2011-8-23
	
	 * @param queryString 关键词
	 * @return datagrid的JSON
	 */
	public static String searchNews(String queryString) {
		
		String result = "[]";
		String path = System.getProperty("user.dir") + "\\workspace\\S3H3L3\\index";
//		String path = "S3H3L3\\index";
		
		File indexDir = new File(path);
		String[] fields = { "id", "title", "author", CONTENT, "digest", "keyword", "time" };
		IndexSearcher searcher = null;
		List<News> resultList = new ArrayList<News>();
		try {
			Directory dir = new SimpleFSDirectory(indexDir);
			searcher = new IndexSearcher(dir);
			searcher.setSimilarity(new IKSimilarity());
			Query query = IKQueryParser.parseMultiField(fields, queryString);
			TopDocs tops = searcher.search(query, TEN);
			ScoreDoc[] scores = tops.scoreDocs;

			Analyzer analyzer = new IKAnalyzer();
			SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter(
					"<font color=\"red\">", "</font>");
			Highlighter highlighter = new Highlighter(htmlFormatter,
					new QueryScorer(query));
			highlighter.setTextFragmenter(new SimpleFragmenter(YBBS));
			for (int i = 0; i < scores.length; i++) {
				News news = new News();
				Document doc = searcher.doc(scores[i].doc);
				String title = doc.get("title");
				String text = highlighter.getBestFragment(analyzer, CONTENT,
						doc.get(CONTENT));
				if (text == null || text.equals("")) {
					text = doc.get(CONTENT);
				}
				news.setAuthor(doc.get("author"));
				news.setContent(text);
				news.setKeyword(doc.get("keyword"));
				news.setTime(doc.get("time"));
				news.setDigest(text.substring(0,(text.length() > THIRTY ? THIRTY : text.length())));
				news.setId(Integer.valueOf(doc.get("id")));
				news.setTitle(title);
				resultList.add(news);
			}
			
			DataGrid dataGrid = new DataGrid();
			dataGrid.setRows(resultList);
			dataGrid.setTotal(TEN);
			result = JsonUtil.object2Json(dataGrid);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidTokenOffsetsException e) {
			e.printStackTrace();
		} finally {
			if (searcher != null) {
				try {
					searcher.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}
